package com.hxd.otc.module.order.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.my.model.UploadImageModel;
import com.hxd.otc.module.order.activity.SaleOrderDetailActivity;
import com.hxd.otc.module.order.bean.AllOrderBean;
import com.hxd.otc.module.order.bean.BankInfoPoData;
import com.hxd.otc.module.order.contract.SaleOrderDetailContract;
import com.hxd.otc.module.order.model.AllOrderModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.ResourceUtil;

import java.io.File;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 作  者 :  zjl
 * 时  间 :  2018/6/9 12:02
 * 描  述 :  ${订单完成}
 */
public class SaleOrderDetailPresenter extends BasePresenter<SaleOrderDetailContract.View> implements SaleOrderDetailContract.Presenter {

    AllOrderModel model;
    private final UploadImageModel mUploadImageModel;

    public SaleOrderDetailPresenter(SaleOrderDetailContract.View view) {
        super(view);
        model = AllOrderModel.getInstance();
        mUploadImageModel = UploadImageModel.getInstance();
    }

    @Override
    public void start() {
    }

    @Override
    public void doLoadData(String... category) {

        model.getOrderDetailData(new BaseObserver<AllOrderBean.DataBean>() {
            @Override
            public void onNext(String title, @NonNull AllOrderBean.DataBean bean) {
                mView.onSetData(bean);
                doShowNoMore();
            }

            @Override
            public void onError(int errType, String errMessage) {
                doShowNoMore();
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean<AllOrderBean.DataBean>>bindAutoDispose(), category[0]);

    }


    @Override
    public void getOrderBankList(String... category) {
        model.getOrderBankList(new BaseObserver<List<BankInfoPoData>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull List<BankInfoPoData> bankInfoPoData) {
                mView.getOrderBankList(bankInfoPoData);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {

            }

        }, mView.<BaseBean<List<BankInfoPoData>>>bindAutoDispose(), category);
    }

    @Override
    public void doShowNoMore() {
        mView.onHideLoading();
    }

    @Override
    public void cancleDetal(String... category) {

        model.cancleDetal(new BaseObserver() {
            @Override
            public void onNext(String title, Object o) {
                mView.refreshView(SaleOrderDetailActivity.CANCLE_DETAL_SUCCESS);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean>bindAutoDispose(), category[0], category[1]);
    }

    @Override
    public void confirmPayment(String... category) {

        model.receiveOrder(new BaseObserver() {
            @Override
            public void onNext(String title, Object bean) {
                mView.refreshView(SaleOrderDetailActivity.CONFIRM_PAYMENT_SUCCESS);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean>bindAutoDispose(), category);

    }

    @Override
    public void partConfirmOrder(String... category) {

        model.partConfirmOrder(new BaseObserver() {
            @Override
            public void onNext(String title, Object bean) {
                mView.refreshView(SaleOrderDetailActivity.PART_CONFIRM_PAYMENT_SUCCESS);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean>bindAutoDispose(), category[0], category[1]);

    }

    @Override
    public void withdrawalRep(String... category) {

        model.recallOrder(new BaseObserver() {
            @Override
            public void onNext(String title, Object bean) {
                mView.showToast(title);
                mView.refreshView(SaleOrderDetailActivity.WITHDRAW_SUCCESS);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
            }


        }, mView.<BaseBean>bindAutoDispose(), category[0]);


    }

    @Override
    public void uploadImage(File file) {

        mUploadImageModel.uploadImage(file, new Observer<String>() {

            @Override
            public void onError(Throwable e) {
                mView.uploadImageFail(e.getMessage());
            }

            @Override
            public void onComplete() {
            }

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String result) {
                LogUtils.e("===result======="+result);
                mView.showToast(ResourceUtil.getString(R.string.upload_success));
                mView.uploadImageSuccess(result);

            }
        }, mView.<String>bindAutoDispose());
    }

    @Override
    public void fixQRcode(@NonNull String... category) {
        model.fixQRcode(new BaseObserver() {
            @Override
            public void onNext(@NonNull String msg, @NonNull Object o) {
                mView.fixQRcodeSucced(o.toString());
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
                mView.fixQRcodeFail(errMessage);
            }


        }, mView.<BaseBean>bindAutoDispose(), category);
    }
}
